﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;

namespace SearchClient.Views
{
    public partial class LoginPage : Page
    {

        private ServiceReference.WCFServiceClient proxy;

        public LoginPage()
        {
            InitializeComponent();
            textBlockErrors.Text = "";
            proxy = new ServiceReference.WCFServiceClient();
        }
 
        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void btnAccept_Click(object sender, RoutedEventArgs e)
        {          

            String username = textBoxName.Text;
            String pass = textBoxPass.Text;
            if (username == "" || pass == "")
            {
                textBlockErrors.Text = "Campos usuario y contraseña son requeridos";
            }
            else
            {
                proxy.LoginCompleted += new EventHandler<ServiceReference.LoginCompletedEventArgs>(proxy_LoginCompleted);
                proxy.LoginAsync(username, pass);
            
            }
        }

        void proxy_LoginCompleted(object sender, ServiceReference.LoginCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                MessageBox.Show(e.Error.ToString());
                textBlockErrors.Text = "Error conectando con el servicio. Reintente";
            }
            else
            {
                bool result = e.Result;
                if (result)
                {
                    this.Content = new MainPage();
                }
                else
                {
                    textBlockErrors.Text = "Usuario o contraseña invalido. Reingrese";
                }
            }
        }

    
    }


}
